package com.laikan.library.httpvolley.request;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.laikan.library.httpvolley.engine.Requester;
import com.socks.library.KLog;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * post 请求
 *
 * @author anrun
 */
public class HttpPostRequest extends HttpRequest {

    private String mJsonBody;
    private Map<String, String> mMapBody;

    private MultipartEntity entity;
    private List<File> fileParts;
    private String filePartName;

    public HttpPostRequest(Requester requester, Listener<String> listener, ErrorListener errorListener) {
        super(Request.Method.POST, requester.getUrl(), listener, errorListener);
        mJsonBody = requester.getJsonBody();
        mMapBody = requester.getMapBody();

        File file = requester.getPartFile();
        if (file != null && file.exists()) {
            fileParts = new ArrayList<>();
            fileParts.add(file);
            filePartName = requester.getPartFileName();
            buildMultipartEntity();
        }
    }

    // 设置请求参数
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {

        // TODO Auto-generated method stub
        return mMapBody == null ? super.getParams() : mMapBody;
    }

    public String getmJsonBody() {

        return mJsonBody;
    }

    public Map<String, String> getRequestParams() {

        return mMapBody;
    }

    private void buildMultipartEntity() {
        if (entity == null) {
            entity = new MultipartEntity();
        }
        if (fileParts != null && fileParts.size() > 0) {
            for (File file : fileParts) {
                entity.addPart(filePartName, new FileBody(file));
            }
            long l = entity.getContentLength();
            KLog.e("共计"+fileParts.size() + "个文件，长度：" + l);
        }

        try {
            if (mMapBody != null && mMapBody.size() > 0) {
                for (Map.Entry<String, String> entry : mMapBody.entrySet()) {
                    entity.addPart(entry.getKey(), new StringBody(entry.getValue(), Charset.forName("UTF-8")));
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getBodyContentType() {
        return entity != null ? entity.getContentType().getValue() : super.getBodyContentType();
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        if (entity != null) {

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try {
                entity.writeTo(bos);
            }catch (Exception e) {
                KLog.e("IOException writing to ByteArrayOutputStream");
            }
            return bos.toByteArray();
        }else {
            return mJsonBody == null ? super.getBody() : mJsonBody.getBytes();
        }
    }
}
